package com.zky.system.service.impl;

import com.zky.system.domain.Permission;
import com.zky.system.domain.Role;
import com.zky.system.mapper.RoleMapper;
import com.zky.system.service.IRoleService;
import com.zky.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
@Transactional
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void add(Role role) {
        super.add(role);
        //添加当前角色菜单
        if (role.getMenus()!=null&&role.getMenus().size()>0){
            roleMapper.addRoleMenus(role.getId(),role.getMenus());
        }
        //添加当前角色权限
        if (role.getPermissions()!=null&&role.getPermissions().size()>0){
            roleMapper.addRolePermissions(role.getId(),role.getPermissions());
        }
    }

    @Override
    public void delete(Long id) {
        //删除角色菜单
        roleMapper.deleteMuneByRoleId(id);
        //删除员工和角色中间表
        roleMapper.deleteEmployeeRoleByRoleId(id);
        //删除权限
        roleMapper.deletePermissionByRoleId(id);
        super.delete(id);
    }

    @Override
    public void update(Role role) {
        //删除角色菜单
        roleMapper.deleteMuneByRoleId(role.getId());

        //删除权限
        roleMapper.deletePermissionByRoleId(role.getId());

        //添加当前角色菜单
        if (role.getMenus()!=null&&role.getMenus().size()>0){
            roleMapper.addRoleMenus(role.getId(),role.getMenus());
        }
        //添加当前角色权限
        if (role.getPermissions()!=null&&role.getPermissions().size()>0){
            roleMapper.addRolePermissions(role.getId(),role.getPermissions());
        }
        super.update(role);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);

        }
    }
}
